package Reports;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import Retail.RetailProduct;

public class IncomeStatement extends HttpServlet{

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doprocess(req, resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doprocess(req, resp);
	}
	
	private void doprocess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		List<IncomeStatementList> incomelist = new ArrayList<IncomeStatementList>();
		
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail",
					"sa", "");
			PreparedStatement ps = con.prepareStatement("Select t.PRODUCTID \"Productname\",sum(t.SOLD_PRICE) \"Amount\",sum(p.COST_PRICE) \"Cost\" from orders t, product p where p.PRODUCTID=t.PRODUCTID group by t.PRODUCTID");
		   
			ResultSet rs = ps.executeQuery();
			
		
			while (rs.next()) {
				IncomeStatementList list = new IncomeStatementList();
				String PName = rs.getString("Productname");
				BigDecimal PAmount = rs.getBigDecimal("Amount");
				BigDecimal PCost =rs.getBigDecimal("Cost");
				BigDecimal Profit = PAmount.subtract(PCost);
				
				list.setProductID(PName);
				list.setBalance(PAmount);
				list.setCost(PCost);
				list.setProfit(Profit);
				incomelist.add(list);
					} 
			
			ps.close();
			con.close();
			
		        JasperPrint jasperPrint = null;  
		        
		        try {  
		        	JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream("ImcomeStatement.jrxml")); 
		            jasperPrint = JasperFillManager.fillReport(jasperReport,null,  
		                    new JRBeanCollectionDataSource(incomelist));  
		        } catch (JRException e) {  
		            e.printStackTrace();  
		        }  

		        
		        resp.setContentType("application/pdf");
		        OutputStream outStream = resp.getOutputStream();
		        try {
					JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
				} catch (JRException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		        
		        
			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
	
	
	
	
}
